﻿Imports System.Data.OleDb
Public Class addnewcars
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub button_save_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button_save.Click

        Dim newfilename As String
        newfilename = Guid.NewGuid().ToString() & getsurfix(pic_upload.FileName)
        If pic_upload.HasFile Then
            pic_upload.SaveAs(Server.MapPath("carpictures") & "/" & newfilename)
        End If


        Dim sql As String = "INSERT INTO cars (model,production_year,body_type,price,features,official_site_link,car_picture,michelin_stars) VALUES(@a1,@a2,@a3,@a4,@a5,@a6,@a7,@a8)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@a1", txt_model.Text)
        cmd.Parameters.AddWithValue("@a2", txt_productionyear.Text)
        cmd.Parameters.AddWithValue("@a3", dd_body_type.Text)
        cmd.Parameters.AddWithValue("@a4", (dd_price1.Text + txt_price2.Text))
        cmd.Parameters.AddWithValue("@a5", txt_specification.Text)
        cmd.Parameters.AddWithValue("@a6", txt_weblink.Text)
        cmd.Parameters.AddWithValue("@a7", "carpictures/" & newfilename)
        cmd.Parameters.AddWithValue("@a8", rbl_michelinstars.SelectedValue)
        conn.Open()

        If (cmd.ExecuteNonQuery()) Then
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
            MsgBox("Record saved, redirecting....")
            Response.Redirect("list.aspx")
        Else
            MsgBox("Sorry, your record has not been saved.Check and be sure there s no mistake somewhere!!!")
            txt_model.Focus()
            Return
        End If

    End Sub
    Function getsurfix(ByVal fn As String)
        Dim fileparts = fn.Split(".")
        Dim suffix = fileparts(fileparts.Length - 1)
        Return "." & suffix
    End Function
End Class